package com.pqc.tbk.entity.account;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.google.common.collect.Lists;
import com.pqc.tbk.utils.Collections3;

@Entity
@Table(name = "user")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User {
	private Long id;
	private String nick;// 昵称
	private String username;// 用户名（登录）
	private String password;
	private String email;// 绑定邮箱
	private String tel;// 电话
	private String alipay;// 支付宝
	private Long score;// 积分
	private Double money;// 累计返利
	private Double money_rest;// 账户余额
	private String qq;
	private String sex;
	private String school;
	private String head_pic;
	private String state;// 暂时4位，分别表示：账号是否激活，邮箱是否验证，支付宝是否绑定，手机是否绑定，需要添加其它字段状态在后面追加
	private Long inviter_id;// 邀请人id
	private Integer max_amount; // 最大推广数量
	private Integer used_amount;// 已经使用的推广数量
	private Integer level; // 用户层次 admin为0
	private List<Group> groupList = Lists.newArrayList();// 有序的关联对象集合

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(final String nick) {
		this.nick = nick;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(final String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(final String email) {
		this.email = email;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(final String tel) {
		this.tel = tel;
	}

	public String getAlipay() {
		return alipay;
	}

	public void setAlipay(final String alipay) {
		this.alipay = alipay;
	}

	public Long getScore() {
		return score;
	}

	public void setScore(final Long score) {
		this.score = score;
	}

	public Double getMoney() {
		return money;
	}

	public void setMoney(final Double money) {
		this.money = money;
	}

	public Double getMoney_rest() {
		return money_rest;
	}

	public void setMoney_rest(final Double money_rest) {
		this.money_rest = money_rest;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(final String qq) {
		this.qq = qq;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(final String sex) {
		this.sex = sex;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(final String school) {
		this.school = school;
	}

	public String getHead_pic() {
		return head_pic;
	}

	public void setHead_pic(final String head_pic) {
		this.head_pic = head_pic;
	}

	public String getState() {
		return state;
	}

	public void setState(final String state) {
		this.state = state;
	}

	public Long getInviter_id() {
		return inviter_id;
	}

	public void setInviter_id(final Long inviter_id) {
		this.inviter_id = inviter_id;
	}

	public Integer getMax_amount() {
		return max_amount;
	}

	public void setMax_amount(final Integer max_amount) {
		this.max_amount = max_amount;
	}

	public Integer getUsed_amount() {
		return used_amount;
	}

	public void setUsed_amount(final Integer used_amount) {
		this.used_amount = used_amount;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(final Integer level) {
		this.level = level;
	}

	/**
	 * 账号是否激活
	 * 
	 * @return
	 */
	@Transient
	public boolean getUserActive() {
		return state.charAt(0) == '1';
	}

	/**
	 * 邮箱是否绑定
	 * 
	 * @return
	 */
	@Transient
	public boolean getEmailActive() {
		return state.charAt(1) == '1';
	}

	/**
	 * 支付宝有没有绑定
	 * 
	 * @return
	 */
	@Transient
	public boolean getAlipayActive() {
		return state.charAt(2) == '1';
	}

	/**
	 * 手机是否绑定
	 * 
	 * @return
	 */
	@Transient
	public boolean getTelActive() {
		return state.charAt(3) == '1';
	}

	// 多对多定义
	@ManyToMany
	// 中间表定义,表名采用默认命名规则
	@JoinTable(name = "acct_user_group", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "group_id") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Group> getGroupList() {
		return groupList;
	}

	public void setGroupList(final List<Group> groupList) {
		this.groupList = groupList;
	}

	/**
	 * 用户拥有的权限组名称字符串, 多个权限组名称用','分隔.
	 */
	// 非持久化属性.
	@Transient
	public String getGroupNames() {
		return Collections3.extractToString(groupList, "name", ", ");
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
